package cn.innosmart.aq.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import cn.innosmart.aq.R;
import cn.innosmart.aq.bean.WorkLog;

/**
 * Created by David on 2015/7/24.
 */
public class WorkLogListAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context mContext;
    private ArrayList<WorkLog> workLogs;
    private View.OnClickListener onClickListener;

    /**
     * 日志列表适配器
     *
     * @param context
     */
    public WorkLogListAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        mContext = context;
        workLogs = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return workLogs.size();
    }

    @Override
    public Object getItem(int position) {
        return workLogs.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.listview_work_log, null);
            holder.tvLogTitle = (TextView) convertView.findViewById(R.id.tv_log_title);
            holder.ivLogIcon = (ImageView) convertView.findViewById(R.id.iv_log_icon);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        WorkLog workLog = workLogs.get(position);
        Date date = new Date(workLog.getTime() * 1000);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd  HH:mm");
        holder.tvLogTitle.setText(simpleDateFormat.format(date));
        return convertView;
    }

    /**
     * 刷新数据，更新UI
     * @param workLogs
     */
    public void refresh(ArrayList<WorkLog> workLogs) {
        this.workLogs = workLogs;
        notifyDataSetChanged();
    }


    public void setOnItemClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    //增加日志
    public void add(WorkLog workLog) {
        workLogs.add(workLog);
        notifyDataSetChanged();
    }


    static class ViewHolder {
        TextView tvLogTitle;
        ImageView ivLogIcon;
    }

}
